<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/9/5
 * Time: 下午5:57
 */
namespace Wnkj;
use app\common\model\CardGeneral;
use app\common\model\CardUserGet;

class WnObj
{
    private $cardlist;
    private $openid;



    public function __construct($options)
    {
        $this->cardlist = isset($options['cardlist']) ? $options['cardlist'] : '';
        $this->openid = isset($options['openid']) ? $options['openid'] : '';

    }

      /*新的发券过滤方法
       *
       * 2018/09/05
       * 参数示例 '[{"zs_card_id":"pCVCLvzZ2OXV7tbR76AW8VvemV8M","zs_no":"1"},{"zs_card_id":"pCVCLvwAy-tfArH_YHdlmRfczwhw","zs_no":"2"}]'
       *
       *  */
    public function isgetputsume_rec()
    {
        $re_arrs = "";
        try {
            if(!$this->cardlist) return false;
            $cardlist=$this->cardlist;
            $cardlists = array_column($cardlist, 'zs_card_id');
            foreach ($cardlist as $k => $v) {
                $arrs[$v['zs_card_id']]['card_id'] = $v['zs_card_id'];
                $arrs[$v['zs_card_id']]['zs_no'] = $v['zs_no'];
            }
            $map['card_id'] = array('in', implode(',', $cardlists));
            $CardGeneral = new CardGeneral();
            $card_list_info = $CardGeneral->where($map)->field('card_id,stocks,time_type,end_timestamp')->select();
            foreach ($card_list_info as $k => $v) {
                if (isset($arrs[$v['card_id']])) {
                    $card_list_info[$k]['zs_no'] = $arrs[$v['card_id']]['zs_no'];
                    if ($v['stocks'] >= $arrs[$v['card_id']]['zs_no']) {

                        if ($v['time_type'] == "DATE_TYPE_FIX_TIME_RANGE") {
                            if ($v['end_timestamp'] < time()) {
                                unset($card_list_info[$k]);
                            }
                        }
                    } else {
                        unset($card_list_info[$k]);
                    }
                }
            }
            $re_arrs=collection($card_list_info)->toArray();
            return $re_arrs;
        } catch (\Exception $e) {
            $re_arrs = "";
            return $re_arrs;
        }
    }
    /*新的开卡发券方法
     *
     * 2018/09/03
     * 参数示例 [{"zs_card_id":"pCVCLvzZ2OXV7tbR76AW8VvemV8M","put_sume":"1","get_sume":1,"id":"id"}]
     *  $map['top_mid'] = $param['momach_id'];
     *  $map['card_id'] = $param['card_id'];
     *  $map['openid'] = $openid;
     *  $map['put_type'] = 'fire_card';
     *  $cardlist = $CardGivecardLog->where($map)->field('id,zs_card_id,put_sume,get_sume')->select();
     * */
    public function isgetputsume_biz()
    {
        $arrs = "";

        try {
            if(!$this->cardlist || !$this->openid) return false;
            $cardlist=$this->cardlist;
            $openid=$this->openid;
            if (!empty($cardlist)) {
                $card_id = implode(',', array_column($cardlist, 'zs_card_id'));
                $ids = array_column($cardlist, 'id');
                array_walk($ids, function (&$value) {
                    $value = "giveRecId_$value";
                });
                $outerstr = implode(',', $ids);
                foreach ($cardlist as $k => $v) {
                    $arrs[$v['zs_card_id']]['card_id'] = $v['zs_card_id'];
                    $arrs[$v['zs_card_id']]['outer_str'] = "giveRecId_" . $v['id'];
                    $arrs[$v['zs_card_id']]['put_sume'] = $v['put_sume'];
                    $arrs[$v['zs_card_id']]['openid'] = $openid;
                }
                $CardUserGet = new CardUserGet();
                $map['card_id'] = array('in', $card_id);
                $map['outerstr'] = array('in', $outerstr);
                $map['openid'] = $openid;
                $get_sume = $CardUserGet->where($map)->field('count(id) as get_sume,card_id')->group('card_id')->select();
                foreach ($get_sume as $k => $v) {
                    $val = $arrs[$v['card_id']];
                    if (isset($val)) {
                        if ($val['put_sume'] <= $v['get_sume']) {
                            unset($arrs[$v['card_id']]);
                        }

                    }
                }
            }
            return $arrs;
        } catch (\Exception $e) {

            return $arrs;
        }
    }
}